package com.projectplace.api;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


import org.apache.http.HttpResponse;
import android.os.Handler;
import com.projectplace.api.Callback.Callback;
import com.projectplace.api.CallbackWrapper.CallbackWrapper;
import com.projectplace.api.getInfoHandler;

/**
 * All api's for conversations. Typically you want to first getProjectConversations to get 
 * the first post(the post that created the conversation)
 * of each conversation. This function will return the 50 latest conversation of the project 
 * sorted on latest post time(which is updated on comments aswell)
 * @author johanma
 *
 */

public class ConversationsApi implements Api {
	
	//So you can't initiate this class, makes it singelton
	private ConversationsApi(){ super(); }
	
	public static void getProjectConversations(PPClient client,final Handler handler, String projectID, String timeStamp,final Callback.ResponseListenerProjectConversations response){
		String fetchUri =  PROJECTPLACE_BASIC_API + "project/"+projectID+"/conversations.json?newer_than="+timeStamp; // +projectID +"/conversations.json";
		PPClient.sendRequest(fetchUri, new getInfoHandler(){
			public void onFinish(HttpResponse user, Error err){
				CallbackWrapper.GetProjectConversations tempWrap = new CallbackWrapper.GetProjectConversations(response);
				tempWrap.setResponse(user,err);
				handler.post(tempWrap);
			}
		});
	}
	
	public static void getConversation(PPClient client,final Handler handler, String conversationID,final Callback.ResponseListenerConversation response){
		String fetchUri = PROJECTPLACE_BASIC_API + "conversation/" +conversationID +"/posts.json";
		PPClient.sendRequest(fetchUri, new getInfoHandler(){
			public void onFinish(HttpResponse user, Error err){
				CallbackWrapper.GetConversation tempWrap = new CallbackWrapper.GetConversation(response);
				tempWrap.setResponse(user,err);
				handler.post(tempWrap);
			}
		});
	}
	
}
